<!DOCTYPE html>
<html>
<head>
<title>Newton's Rings Setup</title>
</head>
<body>
<canvas id="myCanvas" width="600" height="400" style="border:1px solid #d3d3d3;"></canvas>
<script>
    const canvas = document.getElementById('myCanvas');
    const ctx = canvas.getContext('2d');

    // Style settings to emulate the original image
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.lineWidth = 3;
    ctx.lineCap = 'round'; 

    // --- 1. Draw Glass Plate ---
    const plate = { x: 50, y: 300, width: 500, height: 50 };
    ctx.beginPath();
    ctx.rect(plate.x, plate.y, plate.width, plate.height);
    ctx.stroke();

    // --- 2. Draw Plano-Convex Lens ---
    // Lens parameters are chosen for visual similarity to the diagram, not to scale.
    const lens = {
        touchX: 300,        // Horizontal center and touch point
        touchY: plate.y,    // Touches the top of the plate
        width: 400,         // Visual width of the lens
        topY: 200           // Y-coordinate of the flat top surface
    };
    const halfWidth = lens.width / 2;
    const height = lens.touchY - lens.topY;

    // Calculate the radius of curvature for the drawing using the circular segment formula:
    // R = ( (w/2)^2 + h^2 ) / (2h)
    const drawRadius = (Math.pow(halfWidth, 2) + Math.pow(height, 2)) / (2 * height);
    
    // Calculate the center of the circle for the arc
    const centerX = lens.touchX;
    const centerY = lens.touchY - drawRadius;

    // Calculate start and end angles for the arc
    const startAngle = Math.atan2(lens.topY - centerY, (lens.touchX + halfWidth) - centerX);
    const endAngle = Math.atan2(lens.topY - centerY, (lens.touchX - halfWidth) - centerX);

    // Draw the lens shape
    ctx.beginPath();
    ctx.arc(centerX, centerY, drawRadius, startAngle, endAngle); // Curved bottom
    ctx.lineTo(lens.touchX - halfWidth, lens.topY); // Connects to the left end for the flat top
    ctx.stroke();

    // --- 3. Draw Radius Arrow and Label ---
    // Define the arrow's start point
    const arrowStart = { x: 400, y: 110 };
    // Calculate the arrow's end point so it lies on the lens curve
    const arrowEnd = { x: 450 };
    arrowEnd.y = centerY + Math.sqrt(Math.pow(drawRadius, 2) - Math.pow(arrowEnd.x - centerX, 2));

    // Draw the arrow line
    ctx.beginPath();
    ctx.moveTo(arrowStart.x, arrowStart.y);
    ctx.lineTo(arrowEnd.x, arrowEnd.y);
    ctx.stroke();

    // Draw the filled arrowhead
    const headLength = 15;
    const angle = Math.atan2(arrowEnd.y - arrowStart.y, arrowEnd.x - arrowStart.x);
    ctx.beginPath();
    ctx.moveTo(arrowEnd.x, arrowEnd.y);
    ctx.lineTo(arrowEnd.x - headLength * Math.cos(angle - Math.PI / 6), arrowEnd.y - headLength * Math.sin(angle - Math.PI / 6));
    ctx.lineTo(arrowEnd.x - headLength * Math.cos(angle + Math.PI / 6), arrowEnd.y - headLength * Math.sin(angle + Math.PI / 6));
    ctx.closePath();
    ctx.fill();

    // Draw the label text "R = 30 cm"
    ctx.font = '22px sans-serif';
    ctx.textAlign = 'left';
    ctx.textBaseline = 'bottom';
    const textPos = {x: 410, y: 105};
    
    // Draw 'R' italicized
    ctx.save();
    ctx.font = 'italic 22px sans-serif';
    ctx.fillText('R', textPos.x, textPos.y);
    ctx.restore();
    
    // Draw the rest of the text
    ctx.fillText(' = 30 cm', textPos.x + 12, textPos.y);

    // --- 4. Draw Figure Caption ---
    ctx.font = 'bold 22px sans-serif';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'bottom';
    ctx.fillText('Fig. 2.20', canvas.width / 2, canvas.height - 15);

</script>
</body>
</html>